﻿using System;
using LitJson;
using UnityEngine;

namespace MGFramework
{
    public class ArchivingBase
    {
        /// <summary>
        /// 将json数据存到本地
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="data">内容</param>
        /// <returns></returns>
        protected static bool SaveJsonData(string key, object data)
        {
            try
            {
                PlayerPrefs.SetString(key, DataEncryption.Encrypt(JsonMapper.ToJson(data)));
                return true;
            }
            catch (Exception e)
            {
                Debug.Log(e);
                return false;
            }
        }

        /// <summary>
        /// 将数据存到本地存档（适用于基本数据类型）
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="data">内容</param>
        /// <returns></returns>
        protected static bool SaveData(string key, object data)
        {
            try
            {
                PlayerPrefs.SetString(key, DataEncryption.Encrypt(data.ToString()));
                return true;
            }
            catch (Exception e)
            {
                Debug.Log(e);
                return false;
            }
        }

        /// <summary>
        /// 从本地存档获取数据
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        protected static string GetStringData(string key, string default_value = null)
        {
            try
            {
                if (PlayerPrefs.HasKey(key))
                {
                    return DataEncryption.Decipher(PlayerPrefs.GetString(key));
                }
                else
                {
                    if (default_value != null) SaveData(key, default_value);
                    return default_value;
                }
            }
            catch
            {
                return default_value;
            }
        }

        /// <summary>
        /// 从本地存档获取数据
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        protected static long GetLongData(string key, long default_value = 0)
        {
            try
            {
                if (PlayerPrefs.HasKey(key))
                {
                    return long.Parse(DataEncryption.Decipher(PlayerPrefs.GetString(key)));
                }
                else
                {
                    SaveData(key, default_value);
                    return default_value;
                }

            }
            catch
            {
                return default_value;
            }
        }

        /// <summary>
        /// 从本地存档获取数据
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        protected static int GetIntData(string key, int default_value = 0)
        {
            try
            {
                if (PlayerPrefs.HasKey(key))
                {
                    return int.Parse(DataEncryption.Decipher(PlayerPrefs.GetString(key)));
                }
                else
                {
                    SaveData(key, default_value);
                    return default_value;
                }
            }
            catch (Exception e)
            {
                Debug.Log(e);
                return default_value;
            }
        }

        /// <summary>
        /// 从本地存档获取数据
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        protected static ushort GetUshortData(string key, ushort default_value = 0)
        {
            try
            {
                if (PlayerPrefs.HasKey(key))
                {
                    return ushort.Parse(DataEncryption.Decipher(PlayerPrefs.GetString(key)));
                }
                else
                {
                    SaveData(key, default_value);
                    return default_value;
                }
            }
            catch
            {
                return default_value;
            }
        }

        /// <summary>
        /// 从本地存档获取数据
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        protected static float GetFloatData(string key, float default_value = 0)
        {
            try
            {
                if (PlayerPrefs.HasKey(key))
                {
                    return float.Parse(DataEncryption.Decipher(PlayerPrefs.GetString(key)));
                }
                else
                {
                    SaveData(key, default_value);
                    return default_value;
                }
            }
            catch
            {
                return default_value;
            }
        }

        /// <summary>
        /// 获取json类型存档
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">主键</param>
        /// <returns></returns>
        protected static T GetJsonData<T>(string key)
        {
            try
            {
                if (PlayerPrefs.HasKey(key))
                {
                    string data = DataEncryption.Decipher(PlayerPrefs.GetString(key));
                    T temp = JsonMapper.ToObject<T>(data);
                    return temp;
                }
                else
                {
                    return default(T);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
                return default(T);
            }
        }

        /// <summary>
        /// 获取json类型存档
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="default_value"></param>
        /// <returns></returns>
        protected static T GetJsonData<T>(string key, T default_value)
        {
            try
            {
                if (PlayerPrefs.HasKey(key))
                {
                    string data = DataEncryption.Decipher(PlayerPrefs.GetString(key));
                    return JsonMapper.ToObject<T>(data);
                }
                else
                {
                    return default_value;
                }
            }
            catch
            {
                return default_value;
            }
        }
    }
}